//
//  KRAlbumInfoListCell.m
//  cookBook
//
//  Created by kuroken on 16/1/22.
//  Copyright © 2016年 kuroken. All rights reserved.
//

#import "KRAlbumInfoListCell.h"
#import <PureLayout.h>
#import <UIImageView+WebCache.h>
#import "KRConst.h"
#import "KRAlbumInfoList.h"
#import "KRRecipeLikeBtn.h"

static NSString * const KRAlbumInfoListID = @"albumInfoList";

@interface KRAlbumInfoListCell ()
/** 食谱封面 imageView */
@property (nonatomic, weak) UIImageView *recipeCoverImage;
/** 食谱标题 label */
@property (nonatomic, weak) UILabel *recipeTitleLabel;
/** 作者用户名 label */
@property (nonatomic, weak) UILabel *recipeUserNameLabel;
/** 视频标识 imageView */
@property (nonatomic, weak) UIImageView *recipeVideoImage;
/** 喜欢/收藏 button */
@property (nonatomic, weak) KRRecipeLikeBtn *recipeLikeBtn;
/** 喜欢/收藏数 label */
@property (nonatomic, weak) UILabel *recipeLikeCountLabel;
/** 推荐理由 label */
@property (nonatomic, weak) UILabel *recipeIntroLabel;

@property (nonatomic, weak) UIView *coverView;

@end

@implementation KRAlbumInfoListCell

+ (instancetype)cellWithTableView:(UITableView *)tableView
{
    KRAlbumInfoListCell *cell = [tableView dequeueReusableCellWithIdentifier:KRAlbumInfoListID];
    if (!cell) {
        cell = [[self alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:KRAlbumInfoListID];
    }
    return cell;
}

- (instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier
{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        if (!iOS8) {  //iOS7
            self.contentView.autoresizingMask = UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth;
        }
        
        self.selectionStyle = UITableViewCellSelectionStyleNone;

        [self setupSubview];
        [self addConstraint];
    }
    return self;
}

- (void)setInfoList:(KRAlbumInfoList *)infoList
{
    _infoList = infoList;
 
    [self.recipeCoverImage sd_setImageWithURL:[NSURL URLWithString:infoList.list_cover] placeholderImage:[UIImage imageNamed:@"default_r580-350"]];
    [self.recipeTitleLabel setText:infoList.list_title];
    [self.recipeUserNameLabel setText:[NSString stringWithFormat:@"by %@", infoList.list_userName]];
    self.recipeVideoImage.hidden = !infoList.hasVideo;
    self.recipeLikeBtn.selected = infoList.isLike;
//    self.recipeLikeBtn.selected = YES;
    
    [self.recipeLikeCountLabel setText:[infoList.list_likeCount integerValue] > 999 ? @"999+" : infoList.list_likeCount];

    [self.recipeIntroLabel setText:infoList.list_intro];

}

- (void)setupSubview
{
    /** 食谱封面 imageView */
    UIImageView *recipeCoverImage = [[UIImageView alloc] init];
    recipeCoverImage.userInteractionEnabled = YES;
    recipeCoverImage.contentMode = UIViewContentModeScaleAspectFill;
    recipeCoverImage.clipsToBounds = YES;
    [self.contentView addSubview:recipeCoverImage];
    self.recipeCoverImage = recipeCoverImage;
    
    UIView *coverView = [[UIView alloc] init];
    coverView.backgroundColor = KRColorWithAlpha(0, 0, 0, 0.2);
    [self.recipeCoverImage addSubview:coverView];
    self.coverView = coverView;
    
    /** 食谱标题 label */
    UILabel *recipeTitleLabel = [[UILabel alloc] init];
    [recipeTitleLabel setTextColor:[UIColor whiteColor]];
    [recipeTitleLabel setFont:[UIFont boldSystemFontOfSize:23]];
    [self.recipeCoverImage addSubview:recipeTitleLabel];
    self.recipeTitleLabel = recipeTitleLabel;
    
    /** 作者用户名 label */
    UILabel *recipeUserNameLabel = [[UILabel alloc] init];
    [recipeUserNameLabel setFont:[UIFont systemFontOfSize:15]];
    [recipeUserNameLabel setTextColor:[UIColor whiteColor]];
    [self.recipeCoverImage addSubview:recipeUserNameLabel];
    self.recipeUserNameLabel = recipeUserNameLabel;
    
    /** 视频标识 imageView */
    UIImageView *recipeVideoImage = [[UIImageView alloc] init];
    [recipeVideoImage setImage:[UIImage imageNamed:@"ico_video_big"]];
    [self.recipeCoverImage addSubview:recipeVideoImage];
    self.recipeVideoImage = recipeVideoImage;
    
    /** 喜欢/收藏 button */
    KRRecipeLikeBtn *recipeLikeBtn = [[KRRecipeLikeBtn alloc] init];
    [recipeLikeBtn addTarget:self action:@selector(likeBtnClick) forControlEvents:UIControlEventTouchUpInside];
    [self.recipeCoverImage addSubview:recipeLikeBtn];
    self.recipeLikeBtn = recipeLikeBtn;
    
    /** 喜欢/收藏数 label */
    UILabel *recipeLikeCountLabel = [[UILabel alloc] init];
    [recipeLikeCountLabel setTextColor:[UIColor whiteColor]];
    [recipeLikeCountLabel setFont:[UIFont systemFontOfSize:9]];
    [recipeLikeCountLabel setTextAlignment:NSTextAlignmentCenter];
    [recipeLikeCountLabel setBackgroundColor:KRColor(239, 135, 38)];
    [self.recipeCoverImage addSubview:recipeLikeCountLabel];
    self.recipeLikeCountLabel = recipeLikeCountLabel;

    /** 推荐理由 label */
    UILabel *recipeIntroLabel = [[UILabel alloc] init];
    recipeIntroLabel.numberOfLines = 0;
    [self.contentView addSubview:recipeIntroLabel];
    self.recipeIntroLabel = recipeIntroLabel;
}

- (void)likeBtnClick
{
#warning TODO animation 动画
    
    self.infoList.list_like = !self.infoList.isLike;
    self.recipeLikeBtn.selected = !self.recipeLikeBtn.isSelected;
}

- (void)addConstraint
{
    /** 食谱封面 imageView */
    [self.recipeCoverImage autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:10];
    [self.recipeCoverImage autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:15];
    [self.recipeCoverImage autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];
    [self.recipeCoverImage autoSetDimension:ALDimensionHeight toSize:210];
    
    [self.coverView autoPinEdgesToSuperviewEdges];
    
    /** 食谱标题 label */
    [self.recipeTitleLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:40];
    [self.recipeTitleLabel autoPinEdgeToSuperviewEdge:ALEdgeLeft withInset:15];
    [self.recipeTitleLabel autoPinEdge:ALEdgeRight toEdge:ALEdgeLeft ofView:self.recipeLikeBtn withOffset:2];
    
    /** 作者用户名 label */
    [self.recipeUserNameLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.recipeTitleLabel];
    [self.recipeUserNameLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.recipeTitleLabel withOffset:5];
    
    /** 视频标识 imageView */
    [self.recipeVideoImage autoPinEdgeToSuperviewEdge:ALEdgeTop withInset:15];
    [self.recipeVideoImage autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:15];
    [self.recipeVideoImage autoSetDimensionsToSize:self.recipeVideoImage.image.size];
    
    /** 喜欢/收藏 button */
    [self.recipeLikeBtn autoPinEdgeToSuperviewEdge:ALEdgeRight withInset:25];
    [self.recipeLikeBtn autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:20];
    [self.recipeLikeBtn autoSetDimensionsToSize:CGSizeMake(45, 45)];
    self.recipeLikeBtn.layer.cornerRadius = 45 * 0.5;
    self.recipeLikeBtn.clipsToBounds = YES;

    /** 喜欢/收藏数 label */
    [self.recipeLikeCountLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeRight ofView:self.recipeLikeBtn withOffset:-18];
    [self.recipeLikeCountLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.recipeLikeBtn withOffset:-18];
    [self.recipeLikeCountLabel autoSetDimensionsToSize:CGSizeMake(27, 27)];
    [self.recipeLikeCountLabel.layer setCornerRadius:27 * 0.5];
    [self.recipeLikeCountLabel setClipsToBounds:YES];
    
    /** 推荐理由 label */
    [self.recipeIntroLabel autoPinEdge:ALEdgeTop toEdge:ALEdgeBottom ofView:self.recipeCoverImage withOffset:10];
    [self.recipeIntroLabel autoPinEdge:ALEdgeLeft toEdge:ALEdgeLeft ofView:self.recipeCoverImage];
    [self.recipeIntroLabel autoPinEdge:ALEdgeRight toEdge:ALEdgeRight ofView:self.recipeCoverImage];
    [self.recipeIntroLabel autoPinEdgeToSuperviewEdge:ALEdgeBottom withInset:10];
}



- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

@end
